﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Windows.Media.Animation;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for ConfigureWindow.xaml
    /// </summary>
    public partial class ConfigureWindow : Window
    {
        Entities.DeploymentProperty DeployProperty;
        ServiceController.ServiceController serviceController;
        String ServiceName;
        String tempFile;
        private Storyboard startbusy;
        private Storyboard stopbusy;
        public ConfigureWindow(Entities.DeploymentProperty _DeployProperty,String _ServiceName)
        {
            InitializeComponent();
            DeployProperty = _DeployProperty;
            ServiceName = _ServiceName;
            serviceController = new ServiceController.ServiceController();
            serviceController.ChangeConfigFileComplete += new ServiceController.ServiceControllerEventHandler(serviceController_ChangeConfigFileComplete);
            TextBoxConfigrefile.Text = HelperClass.ConvertBase64toString(DeployProperty.Configuration);
            tempFile = TextBoxConfigrefile.Text;
            startbusy = (Storyboard)this.FindResource("StartBusy");
            stopbusy = (Storyboard)this.FindResource("StopBusy");




        }

        void serviceController_ChangeConfigFileComplete(object sender, ServiceController.ServiceControllerEventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
             {
                 HideMask();
                 if (e.error != null)
                 {
                     MessageBox.Show(e.error.Message,"Error",MessageBoxButton.OK,MessageBoxImage.Error);
                 }
                 else
                 {
                     MessageBox.Show("Configuration file updated successfully.","Information",MessageBoxButton.OK,MessageBoxImage.Information);
                     this.DialogResult = true;
                     this.Close();
                 }
             }));
        }

      

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

          
            try
            {
                StreamReader ConfigFile = new StreamReader(TextBoxFileLocation.Text);
                string fileContents = ConfigFile.ReadToEnd();
                ConfigFile.Close();
                TextBoxConfigrefile.Text = fileContents;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message,"Error",MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }

        
        

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog OpenFiledialog = new OpenFileDialog();
            OpenFiledialog.Title = "Select a configuration file";
            OpenFiledialog.Filter = "Configuration files (*.cscfg)|*.cscfg|All files (*.*)|*.*";

            if ((bool)OpenFiledialog.ShowDialog())
            {
                TextBoxFileLocation.Text = OpenFiledialog.FileName.ToString();
                try
                {
                    StreamReader ConfigFile = new StreamReader(TextBoxFileLocation.Text);
                    string fileContents = ConfigFile.ReadToEnd();
                    ConfigFile.Close();
                    TextBoxConfigrefile.Text = fileContents;
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void BtnUpload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StreamReader ConfigFile = new StreamReader(TextBoxFileLocation.Text);
                string fileContents = ConfigFile.ReadToEnd();
                ConfigFile.Close();
                TextBoxConfigrefile.Text = fileContents;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!tempFile.Equals(TextBoxConfigrefile.Text))
            {
                ShowMask();
                serviceController.ChangeConfigurationFileAsync(App.SubscriptionID, App.CertificatePath, ServiceName, DeployProperty.DeploymentSlot, TextBoxConfigrefile.Text, null);
            }
            else 
            {
                MessageBox.Show("No changes detected. There is nothing to save.","Information",MessageBoxButton.OK,MessageBoxImage.Information);

                this.Close();

            }
        }

        private void HideMask()
        {
            startbusy.Stop();
            stopbusy.Begin();
            border2_Copy.Visibility = Visibility.Collapsed;
        }

        private void ShowMask()
        {
            border2_Copy.Visibility = Visibility.Visible;
            startbusy.Begin();
        }
    }
}
